//
// RTPMetaInfoPacket.h:
//   Some defs for RTP-Meta-Info payloads. This class also parses RTP meta info packets

#ifndef __RTP_META_INFO_PACKET_H__
#define __RTP_META_INFO_PACKET_H__

#include <stdlib.h>
#include "StrPtrLen.h"

class CRTPMetaInfoPacket
{
	public:
    
        //
        // RTP Meta Info Fields
        
        enum
        {
            kPacketPosField         = 0, //TW0_CHARS_TO_INT('p', 'p'),
            kTransTimeField         = 1, //TW0_CHARS_TO_INT('t', 't'),
            kFrameTypeField         = 2, //TW0_CHARS_TO_INT('f', 't'),
            kPacketNumField         = 3, //TW0_CHARS_TO_INT('p', 'n'),
            kSeqNumField            = 4, //TW0_CHARS_TO_INT('s', 'q'),
            kMediaDataField         = 5, //TW0_CHARS_TO_INT('m', 'd'),
            
            kIllegalField           = 6,
            kNumFields              = 6
        };
               
        //
        // Values for the Frame Type Field  
        enum
        {
            kUnknownFrameType   = 0,
            kKeyFrameType       = 1,
            kBFrameType         = 2,
            kPFrameType         = 3
        };
		

        //
        // Special field IDs
        enum
        {
            kUncompressed = -1,     // No field ID (not a compressed field)
            kFieldNotUsed = -2      // This field is not being used
        };

        //
        // Types
        typedef UInt16 FieldIndex;
        typedef UInt16 FieldName;
        typedef Int32 FieldID;
        typedef UInt16 FrameTypeField;
        
        //
        // Routine that converts the above enum items into real field names
        static FieldName GetFieldNameForIndex(FieldIndex inIndex) 
		{ 
				return kFieldNameMap[inIndex]; 
		}


        static FieldIndex GetFieldIndexForName(FieldName inName);
        
        //
        // Routine that constructs a standard FieldID Array out of a x-RTP-Meta-Info header
        static void ConstructFieldIDArrayFromHeader(StrPtrLen* inHeader, FieldID* ioFieldIDArray);
        



        
        
        //
        // CONSTRUCTOR
        
        CRTPMetaInfoPacket() :  m_PacketBuffer(NULL),
                                m_PacketLen(0),
                                m_TransmitTime(0),
                                m_FrameType(kUnknownFrameType),
                                m_PacketNumber(0),
                                m_PacketPosition(0),
                                m_MediaDataP(NULL),
                                m_MediaDataLen(0),
                                m_SeqNum(0)          {}
        ~CRTPMetaInfoPacket() {}
        

        //
        // Call this to parse the RTP-Meta-Info packet.
        // Pass in an array of FieldIDs, make sure it is kNumFields in length.
        // This function will use the array as a guide to tell which field IDs in the
        // packet refer to which fields.
        Bool  ParsePacket(UInt8* inPacketBuffer, UInt32 inPacketLen, FieldID* inFieldIDArray);
        
        //
        // Call this if you would like to rewrite the Meta-Info packet
        // as a normal RTP packet (strip off the extensions). Note that
        // this will overwrite data in the buffer!
        // Returns a pointer to the new RTP packet, and its length
        UInt8*          MakeRTPPacket(UInt32* outPacketLen);
        
        //
        // Field Accessors
        Int64          GetTransmitTime()       { return m_TransmitTime; }
        FrameTypeField  GetFrameType()          { return m_FrameType; }
        UInt64          GetPacketNumber()       { return m_PacketNumber; }
        UInt64          GetPacketPosition()     { return m_PacketPosition; }
        UInt8*          GetMediaDataP()         { return m_MediaDataP; }
        UInt32          GetMediaDataLen()       { return m_MediaDataLen; }
        UInt16          GetSeqNum()             { return m_SeqNum; }
    
    private:
    
        
        Int64          m_TransmitTime;
        FrameTypeField  m_FrameType;
        UInt64          m_PacketNumber;
        UInt64          m_PacketPosition;

		//RTP Packet
        UInt8*          m_PacketBuffer;
        UInt32          m_PacketLen;


        //RTP MEDIA DATA
        UInt8*          m_MediaDataP;
        UInt32          m_MediaDataLen;

        //RTP CSeqNum
        UInt16          m_SeqNum;
        
        static const FieldName kFieldNameMap[];
        static const UInt32 kFieldLengthValidator[];
};

#endif // __RTP_META_INFO_PACKET_H__
